package com.pcbly.web.components.utils.template;

import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.pcbly.web.bean.client.CustomerListBean; 

/**
 * 模块生成器
 * 
 * <p>
 * 用于生成JEE模块。
 * <p>
 * 包括JAVA类：action,dao,dao.impl,manager,manager.impl；
 * 配置文件：action配置,spring配置；ftl页面：list.html,add.html,edit.html。
 * 数据验证文件：Act-Com_save
 * -validation.xml,Act-Com_edit-validation.xml,Act-Com_update-validation.xml
 * <p>
 * 可设置的参数有：模块实体类名、java类包地址、配置文件地址、ftl页面地址。
 * 
 * 
 */
public class ModuleToHbm {

	protected StringBuilder productHbm() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\"  encoding=\"UTF-8\"?>\r\n");
		sb.append("<!DOCTYPE hibernate-mapping PUBLIC \r\n");
		sb.append("  \"-//Hibernate/Hibernate Mapping DTD 3.0//EN\" \r\n");
		sb
				.append(" \"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">\r\n");
		sb.append("<hibernate-mapping package=\"");
		sb.append(pachageString);
		sb.append("\">\r\n");
		sb.append("	<class name=\"");
		sb.append(className);
		sb.append("\" table=\"t_");
		sb.append(entityL);
		sb.append("_");
		sb.append(entity);
		sb.append("\">\r\n");
		sb
				.append("		<id name=\"id\" type=\"string\" column=\"ID\" length=\"63\">\r\n");
		sb.append("			<generator class=\"uuid.hex\" />\r\n");
		sb.append("		</id>\r\n");
		sb
				.append("		<property update=\"false\" column=\"CREATORID\" length=\"63\" name=\"createUserId\" type=\"string\" />\r\n");
		sb
				.append("		<property update=\"false\" column=\"CREATETIME\" name=\"createDate\" type=\"timestamp\" />\r\n");
		sb
				.append("		<property update=\"false\" column=\"RECYCLED\" length=\"1\" name=\"recycled\" type=\"string\" />\r\n");
		if (!(arrayString.isEmpty() || arrayString == null)) {
			for (String string : arrayString) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"string\" />\r\n");
			}

		}
		if (!(arrayInt.isEmpty() || arrayInt == null)) {
			for (String string : arrayInt) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"int\" />\r\n");
			}

		}
		if (!(arrayDouble.isEmpty() || arrayDouble == null)) {
			for (String string : arrayDouble) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"double\" />\r\n");
			}
		}
		if (!(arrayFloat.isEmpty() || arrayFloat == null)) {
			for (String string : arrayFloat) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"float\" />\r\n");
			}
		}
		if (!(arrayDate.isEmpty() || arrayDate == null)) {
			for (String string : arrayDate) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"timestamp\" />\r\n");
			}
		}
		if (!(arrayBool.isEmpty() || arrayBool == null)) {
			for (String string : arrayBool) {
				sb.append("		<property  column=\"");
				sb.append(string.toUpperCase());
				sb.append("\" name=\"");
				sb.append(string);
				sb.append("\"  type=\"string\" length=\"1\" />\r\n");
			}
		}
		sb.append("	</class>\r\n</hibernate-mapping>\r\n");
		return sb;
	}

	public String getPachageString() {
		return pachageString;
	}

	public void setPachageString(String pachageString) {
		this.pachageString = pachageString;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	protected String getFilePath(String name) {
//		System.out.println("replace:" + pachageString);
		String path = pachageString.replaceAll("\\.", "/");
//		System.out.println("after relpace:" + path);
		return "src/" + path + "/" + name;
	}

	@SuppressWarnings("unchecked")
	public void analyseClass(Class clz) {
		try {
			
			
			// System.out.println(clz.getName());
			// System.out.println(clz.getPackage().getName()); 
			File fileXML;
			pachageString = clz.getPackage().getName();
						// System.out.println(fi);
						// System.out.println(fi.substring(0,fi.indexOf("."))); 
			Field[] fields = clz.getDeclaredFields(); 
			String te = clz.getName();
			String[] args = te.split("\\.");
			this.entityL = args[args.length - 2];
			
			te = te.substring(te.lastIndexOf(".")+1, te.length());
			className = te;
			
			setList(fields);
//						System.out.println(te);
			String name=te.substring(0,te.indexOf("Bean"));
			entity=name.toLowerCase();
			name=name+".hbm.xml";
//						
			name=getFilePath(name);
			System.out.println("<value>"+name+"</value>");
			fileXML = new File(name);
			StringBuilder sb=productHbm();
			
			FileUtils.writeStringToFile(fileXML, sb.toString(), "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void setList(Field[] fields) {
		arrayString = new ArrayList<String>();
		arrayInt = new ArrayList<String>();
		arrayDouble = new ArrayList<String>();
		arrayFloat = new ArrayList<String>();
		arrayDate = new ArrayList<String>();
		arrayBool = new ArrayList<String>();
		for (Field field : fields) {
//			System.out.println(".............");
//			System.out.println(field.getName());
//			System.out.println(field.getType().getName());
			String typeName = field.getType().getName();
			typeName = typeName.toLowerCase();
			if (typeName.indexOf("string") >= 0) {
				arrayString.add(field.getName());
			} else if (typeName.indexOf("int") >= 0) {
				arrayInt.add(field.getName());
			} else if (typeName.indexOf("double") >= 0) {
				arrayDouble.add(field.getName());
			} else if (typeName.indexOf("float") >= 0) {
				arrayFloat.add(field.getName());
			} else if (typeName.indexOf("date") >= 0) {
				arrayDate.add(field.getName());
			} else if (typeName.indexOf("bool") >= 0) {
				arrayBool.add(field.getName());
			}
		}
	}
	
	public String getEntityL() {
		return entityL;
	}

	public void setEntityL(String entityL) {
		this.entityL = entityL;
	}

	public List<String> getArrayString() {
		return arrayString;
	}

	public void setArrayString(List<String> arrayString) {
		this.arrayString = arrayString;
	}

	public List<String> getArrayInt() {
		return arrayInt;
	}

	public void setArrayInt(List<String> arrayInt) {
		this.arrayInt = arrayInt;
	}

	public List<String> getArrayDouble() {
		return arrayDouble;
	}

	public void setArrayDouble(List<String> arrayDouble) {
		this.arrayDouble = arrayDouble;
	}

	public List<String> getArrayFloat() {
		return arrayFloat;
	}

	public void setArrayFloat(List<String> arrayFloat) {
		this.arrayFloat = arrayFloat;
	}

	public List<String> getArrayDate() {
		return arrayDate;
	}

	public void setArrayDate(List<String> arrayDate) {
		this.arrayDate = arrayDate;
	}

	public List<String> getArrayBool() {
		return arrayBool;
	}

	public void setArrayBool(List<String> arrayBool) {
		this.arrayBool = arrayBool;
	}

	private String entityL;
	private String pachageString;
	private String className;
	private String entity;
	private List<String> arrayString;
	private List<String> arrayInt;
	private List<String> arrayDouble;
	private List<String> arrayFloat;
	private List<String> arrayDate;
	private List<String> arrayBool;

	public static void main(String[] args) {

		ModuleToHbm module = new ModuleToHbm();
		module.analyseClass(CustomerListBean.class);
	}

}
